package com.example.provider.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue queue() {
        // 声明队列
        return new Queue("queue_test");
    }

    @Bean
    public TopicExchange topicExchange() {

        return new TopicExchange("exchange_test");
    }

    @Bean
    public Binding binding() {
        // 绑定交换机和队列
        /**
         * provicer 生产者
         *
         * receiver 消费者
         *
         * rabbitMq
         *  exchange
         *  queue
         *  routeKey
         *
         * provider -> message(传递一个路由 120.qmqp.xxx) -> exchange
         *                                   -> *.amqp.# -> queue -> receiver -> message
         *                                   -> *.aaa.* -> queue
         *                                   -> *.bbb.* -> queue
         *
         */
        return BindingBuilder.bind(queue())
                .to(topicExchange()).with("*.amqp.#");
    }
}